package com.bestcem.xm.common.core.config.grpc;

import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer;
import net.devh.boot.grpc.client.inject.StubTransformer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * gRpc 配置
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/27 15:51
 */
@Configuration
public class GRpcConfig {

    /**
     * 提高 gRpc 入参/返回 消息体大小限制
     *
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     * @date 2021/9/27 16:00
     */
    @Bean
    public StubTransformer call() {
        return (name, stub) -> stub
                .withMaxInboundMessageSize(Integer.MAX_VALUE)
                .withMaxOutboundMessageSize(Integer.MAX_VALUE);
    }

    @Bean
    public GrpcChannelConfigurer grpcChannelConfigurer() {
        return (channelBuilder, name) -> {
            if (channelBuilder instanceof NettyChannelBuilder) {
                ((NettyChannelBuilder) channelBuilder)
                        .maxInboundMetadataSize(Integer.MAX_VALUE);
            }
        };
    }
}
